package com.herench.bunch.clinic.convert;

import ca.uhn.hl7v2.model.GenericPrimitive;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.Varies;
import ca.uhn.hl7v2.model.v24.group.MFN_M04_MF_CDM;
import ca.uhn.hl7v2.model.v24.message.MFK_M01;
import ca.uhn.hl7v2.model.v24.message.MFN_M04;
import ca.uhn.hl7v2.model.v24.segment.*;
import com.herench.bunch.clinic.handler.ProxyMsgCodeConstant;
import com.herench.bunch.clinic.support.ProxyMsgTemplate;
import com.herench.bunch.commons.support.RequestMessage;
import com.herench.bunch.commons.utils.IdGenerator;
import org.dom4j.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Date;

public class PaymentSplitPricingConvert extends AbstractMessageConvert{

    private static final Logger logger=LoggerFactory.getLogger(PaymentSplitPricingConvert.class);
    @Override
    public Document doToMessage(RequestMessage requestMessage) throws Exception {
        Document document = ProxyMsgTemplate.getInMsgTpl(ProxyMsgCodeConstant.PAYMENT_SPLIT_PRICING);
        MFN_M04 hl7Message = (MFN_M04) requestMessage.getHl7Message();
        MFN_M04_MF_CDM mfCdm = hl7Message.getMF_CDM();
        MFE mfe = mfCdm.getMFE();
        int k = mfe.getMfe4_PrimaryKeyValueMFEReps();
        Varies[] varies = mfe.getMfe4_PrimaryKeyValueMFE();
        if(varies!=null && varies.length>0){
            for (Varies var : varies){
                GenericPrimitive primitive = (GenericPrimitive) var.getData();
                String[] temp = primitive.getValue().split("=");
                if (temp.length>1){
                    document.getRootElement().addElement(temp[0]).addText(temp[1] != null ? temp[1] : "");
                }else {
                    document.getRootElement().addElement(temp[0]).addText(" ");
                }
            }
        }
        logger.info("200190 to HIS XML ="+document.asXML());
        return document;


    }

    @Override
    public Message doFromMessage(String message) throws Exception {
        Assert.notNull(message, "message 为空");
        logger.info("200190 from HIS XML = " + message);
        message = message.replace("&#x6;#", "MFK_M01_MESSAGE");
        Document doc = ProxyMsgTemplate.getOutMsgTpl(message);
        MFK_M01 mfk = new MFK_M01();
        mfk.initQuickstart("MFK_M01", "MFK_M01", "A");
        MSH msh = mfk.getMSH();
        initResponseMessageMSH(msh);
        MSA msa = mfk.getMSA();
        String status = doc.getRootElement().elementTextTrim("IsSuccessed");
        msa.getErrorCondition().getCe1_Identifier().setValue(status);
        msa.getErrorCondition().getCe2_Text().setValue(doc.getRootElement().elementTextTrim("ErrorMsg").replace("MFK_M01_MESSAGE", "&#x6;#"));

        if (status.equals("1")) {
            MFI mfi = mfk.getMFI();

            BigDecimal CashPay = new BigDecimal(doc.getRootElement().elementTextTrim("CashPay")).setScale(2, BigDecimal.ROUND_CEILING);
            BigDecimal InsurePay = new BigDecimal(doc.getRootElement().elementTextTrim("InsurePay")).setScale(2, BigDecimal.ROUND_CEILING);
            BigDecimal AccountPay = new BigDecimal(doc.getRootElement().elementTextTrim("AccountPay")).setScale(2, BigDecimal.ROUND_CEILING);

            mfi.getMfi1_MasterFileIdentifier().getCe1_Identifier().setValue("CashPay=" + CashPay);
            mfi.getMfi1_MasterFileIdentifier().getCe2_Text().setValue("InsurePay=" + InsurePay);
            mfi.getMfi1_MasterFileIdentifier().getCe3_NameOfCodingSystem().setValue("AccountPay=" + AccountPay);
            mfi.getMfi1_MasterFileIdentifier().getCe4_AlternateIdentifier().setValue("DivisionID=" + doc.getRootElement().elementTextTrim("DivisionID"));
            mfi.getMfi1_MasterFileIdentifier().getCe5_AlternateText().setValue("Comment=" + doc.getRootElement().elementTextTrim("Comment"));
        }
        return mfk;
    }

    @Override
    public Message generatorReqMessage() throws Exception {

        MFN_M04 message = new MFN_M04();
        message.initQuickstart("MFN_M04", "MFN_M04", "A");
        MSH msh = message.getMSH();
        msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue("200190");
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("SSM");
        msh.getMsh5_ReceivingApplication().getHd1_NamespaceID().setValue("HIS");
        msh.getMsh7_DateTimeOfMessage().getTs1_TimeOfAnEvent().setValue(new Date());
        msh.getMsh10_MessageControlID().setValue(IdGenerator.getUUID());

        MFN_M04_MF_CDM mfCdm = message.getMF_CDM();
        MFE mfe = mfCdm.getMFE();



        /**
         * patientId
         */
        addGenericPrimitive(mfe,"HISPatientID", "400218123152534536");

        /**
         * 医保卡卡号
         */
        addGenericPrimitive(mfe,"MedCardNo", "");

        /**
         * 医保卡密码
         */

        addGenericPrimitive(mfe,"MedCardPwd", "");

        /**
         * 医保类型
         */

        addGenericPrimitive(mfe,"InsurTypeCode", "0");

        /**
         * 门特类型
         */

        addGenericPrimitive(mfe,"MtTypeCode", "");

        /**
         * 挂号ID
         */

        addGenericPrimitive(mfe,"RegID", "4002S1903201108528150");

        /**
         * BusinessFrom 业务来源 * 1手机 2网站  3自助机  4窗口 5电话
         */
        addGenericPrimitive(mfe,"BusinessFrom", "4");

        /**
         * 缴费单号
         */

        addGenericPrimitive(mfe,"ReceiptPayNo", "100719320111134706|100719320111232761|100719320111341580");




        return message;
    }






}
